package com.easonji.attendance.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.easonji.admin.manager.UserManager;
import com.easonji.attendance.mapper.RecordMapper;
import com.easonji.attendance.mapper.RecordStateMapper;
import com.easonji.attendance.model.LeaveState;
import com.easonji.attendance.model.Record;
import com.easonji.attendance.model.RecordState;
import com.easonji.attendance.query.RecordVerifyQuery;
import com.easonji.attendance.service.IRecordService;
import com.easonji.attendance.service.IRecordStateService;
import com.easonji.common.base.query.BaseQuery;
import com.easonji.common.base.service.AbstractBaseService;
import com.easonji.common.factory.PageFactory;
import com.easonji.common.page.PageInfo;
import com.easonji.common.util.JsonUtils;
import com.easonji.common.util.StringUtils;
import com.easonji.core.shiro.ShiroUser;
import com.easonji.core.shiro.util.ShiroUtils;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 补卡 服务实现类
 * </p>
 *
 * @author easonji
 * @since 2018-10-05
 */
@Service
public class RecordServiceImpl extends AbstractBaseService<Record> implements IRecordService {

    @Resource
    private RecordMapper recordMapper;

    @Resource
    private RecordStateMapper recordStateMapper;

    @Resource
    private UserManager userManager;

    @Resource
    private IRecordStateService recordStateService;

    @Override
    protected JSONObject warp2Json(Record record) {
       JSONObject result = JsonUtils.parseObject(record);
        result.put("reUserName",userManager.getUserNameById(record.getReUser()));
        result.put("reStateName",getStateName(record.getReState()));
        result.put("reDate",record.getReDate().substring(0,19));
       return result;
    }


    @Override
    public Object queryList(BaseQuery baseQuery){

        // 分页信息
        Page<Record> page = new PageFactory<Record>().defaultPage();
        // 查询条件
        Wrapper<Record> wrapper = new EntityWrapper<>();

        String beginTime = baseQuery.getBeginTime();
        if (StringUtils.isNotEmpty(beginTime)) {
            wrapper.ge("re_date", beginTime);
        }
        String endTime = baseQuery.getEndTime();
        if (StringUtils.isNotEmpty(endTime)) {
            wrapper.le("re_date", endTime);
        }

        ShiroUser shiroUser = ShiroUtils.getUser();
        //查询本人创建的和上级为本人创建的单子
        wrapper.and("(re_user ={0} or re_create_user={0})",shiroUser.getUserId());

        wrapper.orderBy("re_id",false);

        List<Record> records = recordMapper.selectPage(page, wrapper);

        return new PageInfo(warp(records), page.getTotal());
    }

    @Override
    public Object queryRowById(Integer reId) {
        Record record = recordMapper.selectById(reId);
        return warp(record);
    }

    @Override
    public Integer save(Record record ,List<Map<String,Integer>> Auditors) {

        Integer insertint = recordMapper.insert(record);
        if(insertint==0){
            return 0;
        }

        RecordState recordState = new RecordState();
        recordState.setResAuditor(record.getReCreateUser());
        recordState.setResDatetime(new Date());
        recordState.setResRecord(record.getReId());
        recordState.setResState(1);

        insertint = recordStateMapper.insert(recordState);
        if(insertint==0){
            return 0;
        }

        for(Map<String,Integer> map : Auditors){
            recordState = new RecordState();
            recordState.setResAuditor(map.get("user"));
            recordState.setResPost(map.get("post"));
            recordState.setResRecord(record.getReId());
            recordState.setResState(0);
            insertint = recordStateMapper.insert(recordState);
            if(insertint==0){
                return 0;
            }
        }

        return insertint;
    }

    @Override
    public Integer update(Record recordForm) {
        Record record = new Record();
        BeanUtils.copyProperties(recordForm,record);
        return recordMapper.updateById(record);
    }

    @Override
    public Integer delete(Integer reId) {
        Wrapper<Record> wrapper = new EntityWrapper<>();
        wrapper.eq("re_id", reId);
        return recordMapper.delete(wrapper);
    }

    @Override
    public Object queryVerifyList(RecordVerifyQuery recordVerifyQuery){

        ShiroUser shiroUser = ShiroUtils.getUser();



        // 分页信息
        Page<Record> page = new PageFactory<Record>().defaultPage();
        // 查询条件
        Wrapper<Record> wrapper = new EntityWrapper<>();

        String beginTime = recordVerifyQuery.getBeginTime();
        if (StringUtils.isNotEmpty(beginTime)) {
            wrapper.ge("le_date", beginTime);
        }
        String endTime = recordVerifyQuery.getEndTime();
        if (StringUtils.isNotEmpty(endTime)) {
            wrapper.le("le_date", endTime);
        }

        //如果是2则查 历吏审核的
        if(recordVerifyQuery.getState()!=null && recordVerifyQuery.getState()==2){
            List<Integer> oids = recordStateService.queryRecordIdByauditor(shiroUser.getUserId()); //查询历吏审核的 ID
            if(oids.size()==0){
                wrapper.eq("re_id",0);
            }else{
                wrapper.in("re_id",oids);
            }

        }else{
            wrapper.eq("re_next_auditor",shiroUser.getUserId());
        }



        wrapper.orderBy("re_id",false);

        List<Record> records = recordMapper.selectPage(page, wrapper);



        return new PageInfo(warp(records), page.getTotal());
    }

    private String getStateName(Integer state){
        String stateName = null;

        if(state == null){
            return "";
        }
        switch(state){
            case 0:
                stateName = "申请成功，等待审核";
                break;
            case 1:
                stateName = "线长审核通过，等待主管审核";
                break;
            case 2:
                stateName = "审核通过";
                break;
            case -1:
                stateName = "线长不通过";
                break;
            case -2:
                stateName = "主管不通过";
                break;
            default :
                stateName = "";
        }

        return stateName;
    }

}
